package com.kingdee.eas.custom.hr.attendance.app;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.common.DateUtils;
import com.kingdee.eas.custom.common.server.CanonUtils;
import com.kingdee.eas.custom.hr.attendance.ApproveStateEnum;
import com.kingdee.eas.custom.hr.attendance.AttendanceTimeConfigInfo;
import com.kingdee.eas.custom.hr.attendance.SpecialOperationLogFactory;
import com.kingdee.eas.custom.hr.attendance.SpecialOperationLogInfo;
import com.kingdee.eas.custom.hr.attendance.SpecialOperationMgrEntryInfo;
import com.kingdee.eas.custom.hr.attendance.report.AttendanceTraceReportFactory;
import com.kingdee.eas.custom.hr.attendance.report.AttendanceTraceReportInfo;
import com.kingdee.eas.custom.hr.base.SpecialWorkCodeCollection;
import com.kingdee.eas.custom.hr.base.SpecialWorkCodeFactory;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.hr.rec.util.DateUtil;

public class SpecialOperationLogControllerBean extends
		AbstractSpecialOperationLogControllerBean {
	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.hr.attendance.app.SpecialOperationLogControllerBean");

	@Override
	protected void _generateSpecialOperationLog(Context ctx,
			IObjectValue specialOperationMgrEntry) throws BOSException,
			EASBizException {
		Date currentDate = DateUtil.getCurrentDate();

		AttendanceTimeConfigInfo period = CanonUtils.getAttendancePeriod(ctx,
				currentDate);

		if (period == null) {
			return;
		}

		SpecialWorkCodeCollection specialWorkCodes = SpecialWorkCodeFactory
				.getLocalInstance(ctx).getSpecialWorkCodeCollection();

		SpecialOperationMgrEntryInfo entry = (SpecialOperationMgrEntryInfo) specialOperationMgrEntry;
		SpecialOperationLogInfo logInfo = new SpecialOperationLogInfo();

		logInfo.setPerson(entry.getPerson());
		logInfo.setPeriod(period);
		logInfo.setApproveState(ApproveStateEnum.NOT_APPROVE);
		logInfo.setLogInfo(this.getSpecialOperationLogDayInfo(period));
		logInfo.setResult(this.getSpecialOperationResult(specialWorkCodes));
		this._save(ctx, logInfo);
	}

	private String getSpecialOperationLogDayInfo(AttendanceTimeConfigInfo period) {
		StringBuffer buffer = new StringBuffer();

		Calendar beginDate = Calendar.getInstance();
		Calendar endDate = Calendar.getInstance();

		beginDate.setTime(period.getBeginDate());
		endDate.setTime(period.getEndDate());

		int daysOfMonth = DateUtil.getMonthDays(endDate.get(Calendar.YEAR),
				endDate.get(Calendar.MONTH) + 1);

		endDate.set(Calendar.DAY_OF_MONTH, daysOfMonth);

		int days = DateUtils.getDaysBetweenDate(beginDate.getTime(), endDate
				.getTime());

		for (int i = 0; i < days; i++) {
			buffer.append(beginDate.get(Calendar.DAY_OF_MONTH) + ":,");
			beginDate.add(Calendar.DAY_OF_MONTH, 1);
		}

		if (buffer.length() > 0) {
			buffer.deleteCharAt(buffer.length() - 1);
		}

		return buffer.toString();
	}

	private String getSpecialOperationResult(
			SpecialWorkCodeCollection specialWorkCodes) {
		StringBuffer buffer = new StringBuffer();

		for (int i = 0; i < specialWorkCodes.size(); i++) {
			buffer.append(specialWorkCodes.get(i).getNumber() + ":0;");
		}
		return buffer.toString();
	}

	@Override
	protected void _audit(Context ctx, IObjectCollection collection)
			throws BOSException, EASBizException {
		try {
			CoreBaseCollection colls = new CoreBaseCollection();

			List paramList = new ArrayList();

			StringBuffer sql = new StringBuffer(
					"/*dialect*/UPDATE T_KQ_ATTENDANCETRACEREPORT SET ");

			CoreBaseCollection reports = new CoreBaseCollection();
			CoreBaseCollection logs = new CoreBaseCollection();
			boolean flg = false;
			for (int i = 0; i < collection.size(); i++) {
				SpecialOperationLogInfo info = (SpecialOperationLogInfo) collection
						.getObject(i);
				info.setApproveState(ApproveStateEnum.APPROVE);
				colls.add(info);
				String str = info.getLogInfo();
				String[] loginfos = str.split(",");

				AttendanceTraceReportInfo report = AttendanceTraceReportFactory
						.getLocalInstance(ctx).getAttendanceTraceReportInfo(
								"where person.id='"
										+ info.getPerson().getId().toString()
										+ "' and period.id='"
										+ info.getPeriod().getId().toString()
										+ "' and attendancetype=5");

				for (int j = 0; j < loginfos.length; j++) {
					report.put("d"+(j+1), loginfos[j].split(":",2)[1]);
				}
				reports.add(report);
				logs.add(info);
			}
			SpecialOperationLogFactory.getLocalInstance(ctx).save(logs);
			AttendanceTraceReportFactory.getLocalInstance(ctx).saveBatchData(
					reports);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void _unAudit(Context ctx, IObjectCollection collection)
			throws BOSException, EASBizException {
		CoreBaseCollection colls = new CoreBaseCollection();

		for (int i = 0; i < collection.size(); i++) {
			SpecialOperationLogInfo info = (SpecialOperationLogInfo) collection
					.getObject(i);
			info.setApproveState(ApproveStateEnum.NOT_APPROVE);
			colls.add(info);
		}
		this.saveBatchData(ctx, colls);
	}
}